package com.jiepos.mpos.core.util;

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
/**
 * spring上下文工具类
 * <p>标题: —— 要求能简洁地表达出类的功能和职责</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @author 娄伟峰
 * @version 1.0
 * @since 2017年9月11日 下午2:24:46
 * @history（历次修订内容、修订人、修订时间等）
 */
public class ContextUtils {
	public final static Logger log = LoggerFactory.getLogger(ContextUtils.class);

	private static ApplicationContext applicationContext;

	private static Log LOGGER = LogFactory.getLog(ContextUtils.class);

	public static void setApplicationContext(ApplicationContext applicationContext) {
		synchronized (ContextUtils.class) {
			LOGGER.debug("setApplicationContext, notifyAll");
			ContextUtils.applicationContext = applicationContext;
			ContextUtils.class.notifyAll();
		}
	}

	public static ApplicationContext getApplicationContext() {
		synchronized (ContextUtils.class) {
			while (applicationContext == null) {
				try {
					LOGGER.debug("getApplicationContext, wait...");
					ContextUtils.class.wait(60000);
					if (applicationContext == null) {
						LOGGER.warn("Have been waiting for ApplicationContext to be set for 1 minute", new Exception());
					}
				} catch (InterruptedException ex) {
					LOGGER.debug("getApplicationContext, wait interrupted");
				}
			}
			return applicationContext;
		}
	}

	public static Object getBean(String name) {
		return getApplicationContext().getBean(name);
	}

	public static String getAppName() {
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		ServletContext servletContext = webApplicationContext.getServletContext();
		String appName = servletContext.getContextPath();
		if (StringUtils.isBlank(appName)) {
			return "";
		}
		return appName.substring(1);
	}
	/**
	 * 获取应用根目录路径
	 * @return
	 */
	public static String getRootPath() {
		  String classPath = ContextUtils.class.getResource("").getPath();
		  String rootPath  = "";
		  //windows下
		  if("\\".equals(File.separator)){   
		   rootPath  = classPath.substring(1,classPath.indexOf("mpos.jar"));
		   rootPath = rootPath.replace("/", "\\");
		  }
		  //linux下
		  if("/".equals(File.separator)){   
		   rootPath  = classPath.substring(0,classPath.indexOf("!"));
		   rootPath = rootPath.replace("\\", "/");
		  }
		    rootPath = rootPath.replace("file:", "");
	        rootPath = rootPath.replace("mpos.jar:", "");
	        rootPath = rootPath.replace("mpos.jar", "");
	        rootPath = rootPath.replace(":", "");
	        log.debug("rootPath=================="+rootPath);
	        
		  return rootPath;
		 }
	
	
	public static void main(String args[]){
		String rootPath = "/opt/soft/mpos/file:/opt/soft/mpos/mpos.jar:upload/transsign/20150428";
	    rootPath = rootPath.replace("file:", "");
	    System.out.println("rootPath==="+rootPath);

	}
}
